﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using StockTrader.DAL.IDAL;
using StockTrader.DAL.MSSQLHelper;
using StockTrader.BusinessService.DataContract;

namespace StockTrader.DAL.SQLserver
{
    public class Orders : IOrders
    {
        private SqlConnection _internalConnection;
        private SqlTransaction _internalADOTransaction = null;

        public void BeginADOTransaction()
        {
            if (_internalConnection.State != System.Data.ConnectionState.Open)
                _internalConnection.Open();
            _internalADOTransaction = _internalConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
        }
        public void RollBackTransaction()
        {
            _internalADOTransaction.Rollback();
            _internalADOTransaction = null;
        }
        public void CommitADOTransaction()
        {
            _internalADOTransaction.Commit();
            _internalADOTransaction = null;
        }
        public void Open(string connString)
        {
            try
            {
                if (_internalConnection == null)
                    _internalConnection = new SqlConnection(connString);
                if (_internalConnection.State != ConnectionState.Open)
                    _internalConnection.Open();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public void Close()
        {
            if (_internalConnection != null && _internalConnection.State != System.Data.ConnectionState.Closed)
                _internalConnection.Close();
        }

        #region Parameters
        private const string PARM_ACCOUNTNO = "@ACCOUNTNO";
        private const string PARM_ORDERSIDE = "@ORDERSIDE";
        private const string PARM_SYMBOL = "@SYMBOL";
        private const string PARM_ORDERSPEC = "@ORDERSPEC";
        private const string PARM_ORDERCLASS = "@ORDERCLASS";
        private const string PARM_ORDERMODE = "@ORDERMODE";
        private const string PARM_OPENDATE = "@OPENDATE";
        private const string PARM_EXPIREDDATE = "@EXPIREDDATE";
        private const string PARM_ORDERNO = "@ORDERNO";
        private const string PARM_TIMESTAMP = "@TIMESTAMP";
        private const string PARM_PRICE = "@PRICE";
        private const string PARM_QUANTITY = "@QUANTITY";
        private const string PARM_ORDERTYPE = "@ORDERTYPE";
        #endregion


        #region IOrders Members


        public int InsertNewOrder(string AccountNo, string OrderSide, string Symbol, string OrderSpec, string OrderClass, string OrderMode, DateTime OpenDate, DateTime ExpiredDate, string OrderNo, DateTime TimeStamp, string Price, int Quantity, string OrderType)
        {
            try
            {
                SqlParameter[] parms = new SqlParameter[] { 
                            new SqlParameter(PARM_ACCOUNTNO, SqlDbType.VarChar, 10),
                            new SqlParameter(PARM_ORDERSIDE, SqlDbType.VarChar, 4),
                            new SqlParameter(PARM_SYMBOL, SqlDbType.VarChar, 8),
                            new SqlParameter(PARM_ORDERSPEC, SqlDbType.VarChar, 30),
                            new SqlParameter(PARM_ORDERCLASS, SqlDbType.VarChar,30),
                            new SqlParameter(PARM_ORDERMODE, SqlDbType.VarChar, 30),
                            new SqlParameter(PARM_OPENDATE, SqlDbType.DateTime),
                            new SqlParameter(PARM_EXPIREDDATE, SqlDbType.DateTime),
                            new SqlParameter(PARM_ORDERNO, SqlDbType.VarChar, 20),
                            new SqlParameter(PARM_TIMESTAMP, SqlDbType.DateTime, 10),
                            new SqlParameter(PARM_PRICE, SqlDbType.VarChar, 6),
                            new SqlParameter(PARM_QUANTITY, SqlDbType.Int),
                            new SqlParameter(PARM_ORDERTYPE, SqlDbType.VarChar, 30)
                };
                parms[0].Value = AccountNo;
                parms[1].Value = OrderSide;
                parms[2].Value = Symbol;
                parms[3].Value = OrderSpec;
                parms[4].Value = OrderClass;
                parms[5].Value = OrderMode;
                parms[6].Value = OpenDate;
                parms[7].Value = ExpiredDate;
                parms[8].Value = OrderNo;
                parms[9].Value = TimeStamp;
                parms[10].Value = Price;
                parms[11].Value = Quantity;
                parms[12].Value = OrderType;


                switch (OrderType)
                {
                    case "PLACE":
                        return Convert.ToInt32(SQLHelper.ExecuteScalar(_internalConnection, _internalADOTransaction, CommandType.StoredProcedure, "_ProcessNewOrder", parms));
                    case "CHANGE":
                        return Convert.ToInt32(SQLHelper.ExecuteScalar(_internalConnection, _internalADOTransaction, CommandType.StoredProcedure, "_InsertChangeOrder", parms));
                    case "CANCEL":
                        return Convert.ToInt32(SQLHelper.ExecuteScalar(_internalConnection, _internalADOTransaction, CommandType.StoredProcedure, "_InsertCancelOrder", parms));
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            throw new NotImplementedException();
        }
        


        #endregion
    }
}
